package com.example.lj.uav.view.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.lj.uav.R;
import com.example.lj.uav.bean.PointAction;
import com.example.lj.uav.bean.RoutePoint;
import com.example.lj.uav.utils.DoubleUtils;
import com.example.lj.uav.view.RouteSettingActivity;

import java.util.ArrayList;
import java.util.List;

public class FragmentPointSetting extends Fragment implements View.OnClickListener,RadioGroup.OnCheckedChangeListener,Handler.Callback {
    private ImageView mIvPrePoint;
    private ImageView mIvNextPoint;
    private TextView mPointIndex;
    private ImageButton mIbHeightReduce;
    private ImageButton mIbHeightPlus;
    private TextView mTvHeight;
    private ImageButton mIbAngleReduce;
    private ImageButton mIbAnglePlus;
    private TextView mTvAngle;
    private Spinner mSpDirection;
    private ImageButton mIbPitchAngleReduce;
    private ImageButton mIbPitchAnglePlus;
    private TextView mTvPitchAngle;
    private RelativeLayout mRlPointMotion;
    private TextView mTvLongitude;
    private TextView mTvLatitude;
    private ImageButton mIbLongitudePlus;
    private ImageButton mIbLongitudeReduce;
    private ImageButton mIbLatitudePlus;
    private ImageButton mIbLatitudeReduce;
    private RadioGroup mRgAltitudeMode;
    private RadioButton mRbFollow;
    private RadioButton mRbCustom;



    private RoutePoint point;
    private int position;
    private List<RoutePoint> pointList;
    private double customHeight;
    private double lineHeight;
    private RouteSettingActivity mActivity;
    private List<PointAction> actions;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_single_point_setting,null);
        mActivity = (RouteSettingActivity) getActivity();
        initView(view);
        return view;
    }

    private void initView(View view) {
        mIvPrePoint = view.findViewById(R.id.iv_point_previous);
        mIvNextPoint = view.findViewById(R.id.iv_point_next);
        mIvPrePoint.setOnClickListener(this);
        mIvNextPoint.setOnClickListener(this);
        mPointIndex = view.findViewById(R.id.tv_point_index);
        mIbHeightReduce = view.findViewById(R.id.ib_point_height_reduce);
        mIbHeightPlus = view.findViewById(R.id.ib_point_height_plus);
        mIbHeightReduce.setOnClickListener(this);
        mIbHeightPlus.setOnClickListener(this);
        mTvHeight = view.findViewById(R.id.tv_point_height);


        view.findViewById(R.id.ib_point_angle_reduce).setOnClickListener(this);
        view.findViewById(R.id.ib_point_angle_plus).setOnClickListener(this);
        mTvAngle = view.findViewById(R.id.tv_point_angle);
        mSpDirection = view.findViewById(R.id.sp_point_direction);
        view.findViewById(R.id.ib_pitch_angle_reduce).setOnClickListener(this);
        view.findViewById(R.id.ib_pitch_angle_plus).setOnClickListener(this);
        mTvPitchAngle = view.findViewById(R.id.tv_pitch_angle);
        view.findViewById(R.id.rl_point_motion).setOnClickListener(this);
        mTvLongitude = view.findViewById(R.id.tv_point_longitude);
        mTvLatitude = view.findViewById(R.id.tv_point_latitude);
        view.findViewById(R.id.iv_longitude_plus).setOnClickListener(this);
        view.findViewById(R.id.iv_longitude_reduce).setOnClickListener(this);
        view.findViewById(R.id.iv_latitude_plus).setOnClickListener(this);
        view.findViewById(R.id.iv_latitude_reduce).setOnClickListener(this);

        mRgAltitudeMode = view.findViewById(R.id.rg_point_height_mode);
        mRbFollow = view.findViewById(R.id.rb_height_follow_line_set);
        mRbCustom = view.findViewById(R.id.rb_height_custom_set);
        mRgAltitudeMode.setOnCheckedChangeListener(this);



    }

    public void onPointSelected(int position){
        this.position = position;
        if (position<=0){
            mIvPrePoint.setEnabled(false);
            mIvNextPoint.setEnabled(false);
            mPointIndex.setText("");
            mTvAngle.setText("");
            mTvHeight.setText("");
            mTvLatitude.setText("");
            mTvLongitude.setText("");
            mTvPitchAngle.setText("");
            mTvHeight.setText("");
//            setButtonAllEnable(false);
            return;
        }
        point = pointList.get(position);
        mIvPrePoint.setEnabled(true);
        if (point.getActions()==null){
            point.setActions(new ArrayList<PointAction>());
        }
        if (position == pointList.size()-1){
            mIvNextPoint.setEnabled(false);
        }else{
            mIvNextPoint.setEnabled(true);
        }
        mPointIndex.setText("航点"+(position+1));
        mTvAngle.setText(DoubleUtils.doubleFormat(point.getAngle(),2)+"");
        mTvHeight.setText(DoubleUtils.doubleFormat(point.getAltitude(),2)+"");
        mTvLatitude.setText(DoubleUtils.doubleFormat(point.getLatitude(),7)+"");
        mTvLongitude.setText(DoubleUtils.doubleFormat(point.getLongitude(),7)+"");
        mTvPitchAngle.setText(DoubleUtils.doubleFormat(point.getPitchAngle(),2)+"");
        mTvHeight.setText(DoubleUtils.doubleFormat(point.getAltitude(),2)+"");
        if ("false".equals(point.getUseWaylineAltitude())){
            mRbCustom.setChecked(true);
        }else{
            mRbFollow.setChecked(true);
        }
        actions = point.getActions();
        mActivity.upDateActionInfo(actions);
    }

    private void setButtonAllEnable(boolean enable) {
        mIvNextPoint.setEnabled(enable);
        mIvPrePoint.setEnabled(enable);
        mIbHeightPlus.setEnabled(enable);
        mIbHeightReduce.setEnabled(enable);
        mIbAnglePlus.setEnabled(enable);
        mIbAngleReduce.setEnabled(enable);
        mIbLatitudePlus.setEnabled(enable);
        mIbLatitudeReduce.setEnabled(enable);
        mIbLongitudePlus.setEnabled(enable);
        mIbLongitudeReduce.setEnabled(enable);
        mIbPitchAnglePlus.setEnabled(enable);
        mIbPitchAngleReduce.setEnabled(enable);
        mRbCustom.setEnabled(enable);
        mRbFollow.setEnabled(enable);
    }

    public void setFragmentData(List<RoutePoint> list,double lineHeight){
        this.lineHeight = lineHeight;
        this.pointList = list;
    }

    @Override
    public void onClick(View view) {
        if (position<0){
            return;
        }
        switch (view.getId()){
            case R.id.iv_point_previous:
                position = position-1;
                onPointSelected(position);
                mActivity.setMarkerChecked(position);
                break;
            case R.id.iv_point_next:
                position = position+1;
                onPointSelected(position);
                mActivity.setMarkerChecked(position);
                break;
            case R.id.ib_point_height_reduce:
                customHeight = point.getAltitude();
                customHeight = DoubleUtils.doubleFormat(customHeight-0.1,2);
                point.setAltitude(customHeight);
                mTvHeight.setText(customHeight+"");
                break;
            case R.id.ib_point_height_plus:
                customHeight = point.getAltitude();
                customHeight = DoubleUtils.doubleFormat(customHeight+0.1,2);
                point.setAltitude(customHeight);
                mTvHeight.setText(customHeight+"");
                break;
            case R.id.ib_point_angle_reduce:
                double angle = point.getAngle();
                angle = DoubleUtils.doubleFormat(angle-0.1,2);
                point.setAngle(angle);
                mTvAngle.setText(angle+"");
                break;
            case R.id.ib_point_angle_plus:
                double angle1 = point.getAngle();
                angle1 = DoubleUtils.doubleFormat(angle1+0.1,2);
                point.setAngle(angle1);
                mTvAngle.setText(angle1+"");
                break;
            case R.id.ib_pitch_angle_reduce:
                double pitch = point.getPitchAngle();
                pitch = DoubleUtils.doubleFormat(pitch-0.1,2);
                point.setPitchAngle(pitch);
                mTvPitchAngle.setText(pitch+"");
                break;
            case R.id.ib_pitch_angle_plus:
                double pitch1 = point.getPitchAngle();
                if (pitch1>=0){
                    return;
                }
                pitch1 = DoubleUtils.doubleFormat(pitch1+0.1,2);
                point.setPitchAngle(pitch1);
                mTvPitchAngle.setText(pitch1+"");
                break;
            case R.id.rl_point_motion:
//                mLlActionMenu.setVisibility(View.VISIBLE);
                mActivity.showActionsSelectMenu();
                break;

            case R.id.iv_longitude_plus:
                double longitude = point.getLongitude();
                longitude = DoubleUtils.doubleFormat(longitude + 0.000005,7);
                point.setLongitude(longitude);
                mTvLongitude.setText(longitude+"");
                mActivity.upDatePointAndLine(position);
                break;
            case R.id.iv_longitude_reduce:
                double longitude1 = point.getLongitude();
                longitude1 = DoubleUtils.doubleFormat(longitude1 - 0.000005,7);
                point.setLongitude(longitude1);
                mTvLongitude.setText(longitude1+"");
                mActivity.upDatePointAndLine(position);
                break;
            case R.id.iv_latitude_plus:
                double lat = point.getLatitude();
                lat = DoubleUtils.doubleFormat(lat + 0.000005,7);
                point.setLatitude(lat);
                mTvLatitude.setText(lat+"");
                mActivity.upDatePointAndLine(position);
                break;
            case R.id.iv_latitude_reduce:
                double lat1 = point.getLatitude();
                lat1 = DoubleUtils.doubleFormat(lat1 - 0.000005,7);
                point.setLatitude(lat1);
                mTvLatitude.setText(lat1+"");
                mActivity.upDatePointAndLine(position);
                break;

        }
    }



    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.rb_height_follow_line_set:
                mRbCustom.setTextColor(getResources().getColor(R.color.white));
                mRbFollow.setTextColor(getResources().getColor(R.color.black));
                point.setAltitude(lineHeight);
                mTvHeight.setText(lineHeight+"");
                mIbHeightReduce.setEnabled(false);
                mIbHeightPlus.setEnabled(false);
                point.setUseWaylineAltitude("true");
                break;
            case R.id.rb_height_custom_set:
                if (customHeight==0){
                    customHeight = lineHeight;
                }
                mRbCustom.setTextColor(getResources().getColor(R.color.black));
                mRbFollow.setTextColor(getResources().getColor(R.color.white));
                point.setAltitude(customHeight);
                mTvHeight.setText(customHeight+"");
                mIbHeightReduce.setEnabled(true);
                mIbHeightPlus.setEnabled(true);
                point.setUseWaylineAltitude("false");
                break;
        }
    }

    public void updatePointPosition(int position) {
        onPointSelected(position);
    }

    @Override
    public boolean handleMessage(Message message) {
        return false;
    }
}
